package com.myapp.servlet;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myapp.bean.Company;
import com.myapp.sqlmapper.SQLUtil;
import com.myapp.util.CommUtil;
import com.myapp.util.Consts;
import com.myapp.util.DBUtil;
import com.myapp.util.JsonUtil;

public class GetCompany extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 12628595106370002L;
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		String name = request.getParameter("name");
		String currentPages = request.getParameter("currentPage");
		String pageSizes = request.getParameter("pageSize");
		String sql = "";
		if(CommUtil.isBlank(name)){
			int currentPage = Consts.PAGE_CURRENT;
			int pageSize = Consts.PAGE_SIZE;
			if(!CommUtil.isBlank(pageSizes)){
				pageSize = Integer.parseInt(pageSizes);
			}
			if(!CommUtil.isBlank(currentPages)){
				currentPage = Integer.parseInt(currentPages);
			}
			List<String> pageList = new ArrayList<String>();
			pageList.add(currentPage*pageSize+"");
			pageList.add(pageSize+"");
			sql = SQLUtil.getSql("companyMapper.getCompany", pageList);
		}else{
			List<String> paramList = new ArrayList<String>();
			paramList.add(name);
			sql = SQLUtil.getSql("companyMapper.getCompanyByName", paramList);
		}
		try {
			ResultSet rs = DBUtil.exeQuery(sql);
			List<Company> companyList = new ArrayList<Company>();
			while(rs.next()){
				Company company = new Company();
				company.setId(rs.getString("id"));
				company.setName(rs.getString("name"));
				company.setFullName(rs.getString("full_name"));
				company.setTradeID(rs.getString("trade_id"));
				company.setCompanyDesc(rs.getString("company_desc"));
				company.setAddress(rs.getString("address"));
				company.setCreateid(rs.getString("createid"));
				company.setCreatedt(rs.getString("createdt"));
				company.setUpdaetid(rs.getString("updaetid"));
				company.setUpdatedt(rs.getString("updatedt"));
				companyList.add(company);
			}
			Map<String, Object> mapResult = new HashMap<String, Object>();
			mapResult.put("totalNum", totalNum);
			mapResult.put("listSize", String.valueOf(financeList.size()));
			mapResult.put("list", companyList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		this.doGet(request, response);
	}
}
